package com.zh.api.controller.operation;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.utils.*;
import com.zh.operation.domain.PaymentDO;
import com.zh.operation.service.PaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
 
@Controller
@Api("老人用餐【食谱套餐】")
public class PaymentController extends BaseController {
	@Autowired
	private PaymentService paymentService;

	@Log("食谱套餐列表")
	@ResponseBody
	@RequestMapping(value = "listOperationPayment", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO listOperationPayment(@RequestBody Map<String, Object> params){
		if(CheckEmptyUtil.isEmpty(params)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		//查询列表数据
        Query query = new Query(params);
		List<PaymentDO> paymentList = paymentService.list(query);
		int total = paymentService.count(query);
		PageUtils pageUtils = new PageUtils(paymentList, total);
		return ResultVO.success(pageUtils);
	}

	@Log("编辑套餐")
	@ResponseBody
	@RequestMapping(value = "editOperationPaymentById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO editOperationPaymentById(@RequestBody PaymentDO payment){
		if(payment==null || CheckEmptyUtil.isEmpty(payment.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		PaymentDO paymentDO = paymentService.get(payment.getId());
	    return ResultVO.success(paymentDO);
	}

//	@Log("根据id获取当前所属套餐")
//	@ResponseBody
//	@RequestMapping(value = "getOperationPaymentById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
//	public ResultVO getPayment(@RequestBody PaymentDO payment){
//		if(payment==null || CheckEmptyUtil.isEmpty(payment.getId())){
//			return ResultVO.error(ResultEnum.PARAME_ERROR);
//		}
//		List<PaymentDO> PaymentDOList = paymentService.getPayment(payment.getId());
//		return ResultVO.success(PaymentDOList);
//	}

	@Log("新增套餐")
	@ResponseBody
	@RequestMapping(value = "saveOperationPayment", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO saveOperationPayment(@RequestBody PaymentDO payment){
		if(payment==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(paymentService.save(payment)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	@Log("更新套餐")
	@ResponseBody
	@RequestMapping(value = "updateOperationPaymentById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO updateOperationPaymentById(@RequestBody  PaymentDO payment){
		if(payment==null || CheckEmptyUtil.isEmpty(payment.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		paymentService.update(payment);
		return ResultVO.success();
	}

	@Log("删除套餐")
	@ResponseBody
	@RequestMapping(value = "removeOperationPaymentById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO removeOperationPaymentById(@RequestBody  PaymentDO payment){
		if(payment==null || CheckEmptyUtil.isEmpty(payment.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(paymentService.remove(payment.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}
	@RequestMapping(value = "batchRemoveOperationPaymentByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量删除套餐")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		if(jsonParam==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		List<String> ids= (List<String>)jsonParam.get("ids");
		if(CheckEmptyUtil.isEmpty(ids)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		String[] strs1=ids.toArray(new String[ids.size()]);
		if(paymentService.batchRemove(strs1)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
}
